package com.example.xaop.factorybean;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author xingyulai
 * @date 2024/3/14 3:12 PM
 * @description
 **/
public class MyFactoryBean implements FactoryBean {

    private Class targetClass;

    public MyFactoryBean( String className) {

        try {
            targetClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public Object getObject() throws Exception {

        Object o = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{targetClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return "你好啊";
            }
        });


        return o;
    }

    @Override
    public Class<?> getObjectType() {
        return targetClass;
    }
}
